#ifndef _MYSQLTOOL_H
#define _MYSQLTOOL_H
#include<iostream>
#include<vector>
#include<unordered_map>
#include<set>
#include<mysql_connection.h>
#include<mysql_driver.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace sql;
using std::vector;
using std::unordered_map;
using std::set;
using std::pair;
using std::string;
class Mysql_Tool
{
public:
Mysql_Tool(string DBHOST,string USER,string PASSWORD)
{
this->_driver=get_driver_instance();
this->_conn=this->_driver->connect(DBHOST,USER,PASSWORD);
this->_conn->setSchema("search_engine");
}
void read_invert_index_sql(vector<string>&query_word,unordered_map<string,set<pair<int,double>>>& _inverIndexTable)
{
auto stmt=_conn->createStatement();
string sql_str="select word,doc_id,weight from invertindex where ";
for(size_t i=0;i<query_word.size();++i)
{
    sql_str+="word='";
    sql_str+=query_word[i];
    if(i!=query_word.size()-1)
    {
        sql_str+="' or ";
    }
    else sql_str+="';";
}
auto res=stmt->executeQuery(sql_str);

while(res->next())
{
string result_word=res->getString("word");
int result_doc_id=res->getInt("doc_id");
double result_weight=res->getDouble("weight");
pair<int,double>temp_pair(result_doc_id,result_weight);
_inverIndexTable[result_word].insert(temp_pair);
}
delete res;
delete stmt;
}
~Mysql_Tool()
{
    if(this->_conn!=nullptr)
    {
    delete this->_conn;
    this->_conn=nullptr;
    }
   
}
private:
Driver* _driver;
Connection* _conn;
};
#endif